package leetcode;

import java.util.Arrays;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0213.java
 * @Author : WangRuoyu
 * @Date 2023/11/26 22:19
 */

public class Q0213 {
    public int rob(int[] nums) {
        if (nums.length == 1) return nums[0];
        int[] f = new int[nums.length + 1];
        f[1] = nums[0];
        f[2] = f[1];
        for (int i = 3; i <= nums.length - 1; i++) {
            f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
        }
        int res = f[nums.length - 1];
        Arrays.fill(f, 0);
        for (int i = 2; i <= nums.length; i++) {
            f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
        }
        return Math.max(f[nums.length], res);
    }

    public static void main(String[] args) {
        Q0213 sl = new Q0213();
    }
}